package com.bringway.p2p.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bringway.framework.controller.JsonController;
import com.bringway.framework.util.RequestUtils;
import com.bringway.p2p.service.ComNewsService;

import net.sf.json.JSONObject;


/**  
 * @类名 ComNewsController  
 * @功能 TODO  公司动态新闻 管理类
 * @作者 js 
 * @日期 2016年12月23日 上午9:26:06  
 *    
 */
@Controller
@RequestMapping("/comnews")
public class ComNewsController extends JsonController {
	@Autowired
	private ComNewsService comNewsService;

	/**
	 * @作者 js  
	 * @方法名 queryComNewsList  
	 * @功能 TODO 获取公司动态新闻列表
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryComNewsList")
	public ModelAndView queryComNewsList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String title = RequestUtils.getStringParameter(request, "title");
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		JSONObject params = new JSONObject();
		JSONObject result = new JSONObject();
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("title", title);
		List<Map<String, Object>> list = comNewsService.queryComNewsList(params);
		int count = comNewsService.getComNewsCount(params);
		result.put("data", list);
		result.put("total", count);
		if (null!=page) {//新版本,传参
			int totalPage=count%limit==0?count/limit:count/limit+1;
			result.put("totalPage", totalPage);
		}
		serialize(request, response, result);
		return null;
	}

	/**
	 * @作者 js  
	 * @方法名 insertComNews  
	 * @功能 TODO 新增公司动态新闻
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/insertComNews")
	public ModelAndView insertComNews(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		Map<String, Object> paramMap = new HashMap<>();
		String title = RequestUtils.getStringParameter(request, "title");
		String author = RequestUtils.getStringParameter(request, "author");
		String abs = RequestUtils.getStringParameter(request, "abs");
		String userid = RequestUtils.getStringParameter(request, "userid");
		String texthtml = RequestUtils.getStringParameter(request, "texthtml");
		String showsj = RequestUtils.getStringParameter(request, "showsj");
		paramMap.put("title", title);
		paramMap.put("author", author);
		paramMap.put("abs", abs);
		paramMap.put("userid", userid);
		paramMap.put("texthtml", texthtml);
		paramMap.put("showsj", showsj);
		boolean isSuc = comNewsService.insertComNews(paramMap);
		if (isSuc) {
			result.put("isSuc", true);
		} else {
			result.put("isSuc", false);
			result.put("msg", "新增失败!");
		}
		result.put("success", true);
		serialize(request, response, result);
		return null;
	}

	/**
	 * @作者 js  
	 * @方法名 updateComNews  
	 * @功能 TODO 更新公司动态新闻
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateComNews")
	public ModelAndView updateComNews(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		Map<String, Object> paramMap = new HashMap<>();
		String id = RequestUtils.getStringParameter(request, "id");
		String title = RequestUtils.getStringParameter(request, "title");
		String author = RequestUtils.getStringParameter(request, "author");
		String abs = RequestUtils.getStringParameter(request, "abs");
		String userid = RequestUtils.getStringParameter(request, "userid");
		String texthtml = RequestUtils.getStringParameter(request, "texthtml");
		String showsj = RequestUtils.getStringParameter(request, "showsj");
		paramMap.put("id", id);
		paramMap.put("title", title);
		paramMap.put("author", author);
		paramMap.put("abs", abs);
		paramMap.put("userid", userid);
		paramMap.put("texthtml", texthtml);
		paramMap.put("showsj", showsj);
		boolean isSuc =comNewsService.updateComNews(paramMap);
		if (isSuc) {
			result.put("isSuc", true);
		} else {
			result.put("isSuc", false);
			result.put("msg", "更新失败!");
		}
		result.put("success", true);
		serialize(request, response, result);
		return null;
	}
	/**
	 * @作者 js  
	 * @方法名 publicComNews  
	 * @功能 TODO 公司动态新闻发布管理
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/publicComNews")
	public ModelAndView publicComNews(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		Map<String, Object> paramMap = new HashMap<>();
		String id = RequestUtils.getStringParameter(request, "id");
		String ispub = RequestUtils.getStringParameter(request, "ispub");
		paramMap.put("id", id);
		paramMap.put("ispub", ispub);
		boolean isSuc =comNewsService.publicComNews(paramMap);
			result.put("isSuc", isSuc);
		result.put("success", true);
		serialize(request, response, result);
		return null;
	}

}
